package com.sojson.config.bean;

import static com.sojson.util.SpringUtil.registerBean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.Configuration;

import com.sojson.constant.ConstantByMap;
import com.sojson.enums.core.EEnvironmentType;
import com.sojson.util.security.springsecurity.service.AnnotationService;
import com.sojson.util.security.springsecurity.service.impl.AnnotationServiceClose;
import com.sojson.util.security.springsecurity.service.impl.AnnotationServiceOnline;
import com.sojson.util.security.springsecurity.service.impl.AnnotationServiceTest;

import lombok.extern.slf4j.Slf4j;

/**
 * 手动注册Bean(优先级全场第二高)
 * 
 * @author liu
 * @date 2021-04-28
 */
@Slf4j
@Configuration
public class SecurityBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    /**
     * 在{@link org.springframework.context.annotation.Condition}之后调用<BR/>
     * 在{@link PropertiesPlaceholder}之前调用<BR/>
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        // 注册bean
        log.info("开始手动注册SecurityBean对象！");

        if (EEnvironmentType.TEST.getCode() == ConstantByMap.ENVIRONMENT) {
            if (AnnotationService.isEnabled) {
                registerBean(registry, AnnotationServiceTest.class, AnnotationService.BEAN_NAME);
                log.info("AnnotationServiceTest手动注入成功！");
            } else {
                registerBean(registry, AnnotationServiceClose.class, AnnotationService.BEAN_NAME);
                log.info("AnnotationServiceClose手动注入成功！");
            }
        } else if (EEnvironmentType.ONLINE.getCode() == ConstantByMap.ENVIRONMENT) {
            registerBean(registry, AnnotationServiceOnline.class, AnnotationServiceOnline.BEAN_NAME);
            log.info("AnnotationService手动注入成功！");
        }

        log.info("手动注册SecurityBean对象结束！");
    }

    /**
     * 在{@link PropertiesPlaceholder}之后调用
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }

}